package com.yunmang.http;

import com.yunmang.activity.statictis.SavingQueryStatictics;
import com.yunmang.activity.statictis.SignedStatics;
import com.yunmang.entity.BatchDetailBean;
import com.yunmang.entity.CarListBean;
import com.yunmang.entity.ConfigBean;
import com.yunmang.entity.DeliverBean;
import com.yunmang.entity.ExceptionQueryBean;
import com.yunmang.entity.FahuoStatictics;
import com.yunmang.entity.LoginBean;
import com.yunmang.entity.MemberBean;
import com.yunmang.entity.NameBean;
import com.yunmang.entity.OpenTicketResultBean;
import com.yunmang.entity.OrderDetailBean;
import com.yunmang.entity.PackageBean;
import com.yunmang.entity.ParameterEntity.ConfirmBean;
import com.yunmang.entity.ParameterEntity.NextStationBean;
import com.yunmang.entity.ParameterEntity.OrderLookBean;
import com.yunmang.entity.ParameterEntity.PsSuccessBean;
import com.yunmang.entity.ParameterEntity.SignBean;
import com.yunmang.entity.ParameterEntity.UploadSignBean;
import com.yunmang.entity.SavingStatictics;
import com.yunmang.entity.SendBatchBean;
import com.yunmang.entity.SignStatictics;
import com.yunmang.entity.TraceBean;
import com.yunmang.entity.TransportBean;
import com.yunmang.entity.UpdateVersionBean;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Query;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 设置为单例模式进行访问
 * 封装rxjava+retrofit
 * Created by ltt on 2017/3/21.
 */
public class HttpUtils {
    public static  String BASE_URL = "http://192.168.1.24:10087/api/";
//    public static final String BASE_URL = "http://121.201.5.109:7081/api/";//最慧物流
//    public static final String BASE_URL = "http://114.55.29.223:7076/api/";//龙行天下物流
    private static final int DEFAULT_TIMEOUT = 30;
    private Retrofit retrofit;
    private InterfaceService interfaceService;
    //构造方法私有
    private HttpUtils(){
        //手动创建一个OkHttpClient并设置超时时间
        OkHttpClient.Builder httpClientBuilder= new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                         .writeTimeout(DEFAULT_TIMEOUT,TimeUnit.SECONDS)
                         .readTimeout(DEFAULT_TIMEOUT,TimeUnit.SECONDS);
        retrofit=new Retrofit.Builder().baseUrl(BASE_URL)
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        interfaceService = retrofit.create(InterfaceService.class);
    }

    //在访问HttpUtils时创建单例
    private static class SingletonHolder{
        private static final HttpUtils INSTANCE = new HttpUtils();
    }
    //获取单例
    public static HttpUtils getInstance(){
        return SingletonHolder.INSTANCE;
    }

    /**
     * 执行登录
     * @param subscriber
     * @param params
     */
    public void login(Subscriber<LoginBean> subscriber, Map<String, String> params){
        interfaceService.login(params)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void changePwd(Subscriber<NameBean> subscriber, Map<String, String> params){
        Observable observable = interfaceService.changePwd(params);
        toSubscribe(observable,subscriber);
    }
    /**
     * 开票信息上传
     * @param subscriber
     */
    public void save(Subscriber<OpenTicketResultBean> subscriber, RequestBody body) {
         interfaceService.save(body)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
    public void getAllCar(Subscriber<CarListBean> subscriber){
        Observable observable = interfaceService.getCarList();
        toSubscribe(observable,subscriber);
    }

    public void getAllPackages(Subscriber<PackageBean> subscriber){
        Observable observable = interfaceService.getPackages();
        toSubscribe(observable,subscriber);
    }

    public void getConfig(Subscriber<ConfigBean> subscriber){
        Observable observable = interfaceService.getConfigParams();
        toSubscribe(observable,subscriber);
    }

    public void getMemberInfo(Subscriber<MemberBean> subscriber,String code){
        Observable observable =interfaceService.getMemberInfo(code);
        toSubscribe(observable,subscriber);
    }
    public void getshipper(Subscriber<NameBean> subscriber, String code){
        Observable observable =interfaceService.getshipper(code);
        toSubscribe(observable,subscriber);
    }

    public void getConsignee(Subscriber<NameBean> subscriber, String code){
        Observable observable =interfaceService.getConsignee(code);
        toSubscribe(observable,subscriber);
    }

    /**
     * 请求下载发车数据
     * @param subscriber
     * @param hdbh
     * @param nextid
     * @param userId
     */
    public void getTransportInfo(Subscriber<TransportBean> subscriber, String hdbh,String nextid,String userId ){
        Observable observable =interfaceService.getTransportInfo(hdbh,nextid,userId);
        toSubscribe(observable,subscriber);
    }

    public void getNextStation(Subscriber<NextStationBean> subscriber, String orgId){
        Observable observable =interfaceService.getNextStation(orgId);
        toSubscribe(observable,subscriber);
    }
    public void uploadBillSend(Subscriber<NameBean> subscriber, RequestBody body) {
        Observable observable =interfaceService.billSend(body);
        toSubscribe(observable,subscriber);
    }
    public void orderLook(Subscriber<OrderLookBean> subscriber, String StartDate, String EndDate, String Userid, String Hdbh){
        Observable observable =interfaceService.orderLook(StartDate ,EndDate,Userid,Hdbh);
        toSubscribe(observable,subscriber);
    }
    public void orderInvalid(Subscriber<NameBean> subscriber,RequestBody body){
        Observable observable =interfaceService.orderInvalid(body);
        toSubscribe(observable,subscriber);
    }
    //下载到货信息
    public void billConfirm(Subscriber<ConfirmBean> subscriber,String userId, String hdbh){
        Observable observable =interfaceService.billconfirm(userId,hdbh);
        toSubscribe(observable,subscriber);
    }
    //确认到货信息
    public void uploadConfirm(Subscriber<NameBean> subscriber,RequestBody body){
        Observable observable =interfaceService.uploadConfirm(body);
        toSubscribe(observable,subscriber);
    }
    //下载签收数据
    public void downloadSignInfo(Subscriber<SignBean> subscriber, String takeMethod, String hdbh, String orgId){
        Observable observable =interfaceService.downloadSignInfo(takeMethod,hdbh,orgId);
        toSubscribe(observable,subscriber);
    }
    //上传签收
    public void uploadSignInfo(Subscriber<UploadSignBean> subscriber, RequestBody body){
        Observable observable =interfaceService.uploadSign(body);
        toSubscribe(observable,subscriber);
    }
    public void paisongAlert(Subscriber<PsSuccessBean> subscriber, RequestBody body){
        Observable observable =interfaceService.paisongAlert(body);
        toSubscribe(observable,subscriber);
    }
    //获取派送员
    public void getDeliver(Subscriber<DeliverBean> subscriber, String orgId){
        Observable observable =interfaceService.getDeliverPerson(orgId);
        toSubscribe(observable,subscriber);
    }
    public void getTraceInfo(Subscriber<TraceBean> subscriber, String hdbh){
        Observable observable =interfaceService.getTraceInfo(hdbh);
        toSubscribe(observable,subscriber);
    }
    //下载发货统计
    public void getFahuoStatics(Subscriber<FahuoStatictics> subscriber, String userId, String StartDate,
                                String EndDate, int StartDepartId, int EndDepartId){
        Observable observable =interfaceService.getFahuoStatictics(userId,StartDate,EndDate,StartDepartId,EndDepartId);
        toSubscribe(observable,subscriber);
    }
    public void getSignStatictics(Subscriber<SignStatictics> subscriber, String userId, String StartDate,
                                  String EndDate, int StartDepartId, int EndDepartId){
        Observable observable =interfaceService.getSignStatictics(userId,StartDate,EndDate,StartDepartId,EndDepartId);
        toSubscribe(observable,subscriber);
    }
    public void getSavingStatics(Subscriber<SavingStatictics> subscriber, String userId, int departId, int type){
        Observable observable =interfaceService.getSavingStatictics(userId,departId,type);
        toSubscribe(observable,subscriber);
    }

    public void getOrderDetail(Subscriber<OrderDetailBean> subscriber,String hdbh,String orgid){
        Observable observable =interfaceService.getOrderDetial(hdbh,orgid);
        toSubscribe(observable,subscriber);
    }

    public void addException(Subscriber<NameBean> subscriber,RequestBody body){
        Observable observable =interfaceService.addExcepion(body);
        toSubscribe(observable,subscriber);
    }

    public void getExceptionByStatus(Subscriber<ExceptionQueryBean> subscriber,String StartDate,String EndDate,
                                     String userId,String Hdbh,String DateType){
        Observable observable =interfaceService.getExcptionByStatus(userId,StartDate,EndDate,Hdbh,DateType);
        toSubscribe(observable,subscriber);
    }


    public void handleException(Subscriber<NameBean> subscriber,RequestBody body){
        Observable observable =interfaceService.handleException(body);
        toSubscribe(observable,subscriber);
    }

    public void getSendBatch(Subscriber<SendBatchBean> subscriber, String startDate, String endDate,
                             String userId, String hdbh, String type){
        Observable observable =interfaceService.getSendBatch(userId,startDate,endDate,hdbh,type);
        toSubscribe(observable,subscriber);
    }

    public void getBatchDetailById(Subscriber<BatchDetailBean> subscriber,String userId,int id){
        Observable observable =interfaceService.getBatchDetailById(userId,id);
        toSubscribe(observable,subscriber);
    }

    public void editBatch(Subscriber<NameBean> subscriber,RequestBody body){
        Observable observable =interfaceService.editBatch(body);
        toSubscribe(observable,subscriber);
    }

    public void editBill(Subscriber<NameBean> subscriber,RequestBody body){
        Observable observable =interfaceService.editbill(body);
        toSubscribe(observable,subscriber);
    }

    public void checkVersion(Subscriber<UpdateVersionBean> subscriber){
        Observable observable =interfaceService.checkVersion();
        toSubscribe(observable,subscriber);
    }

    //添加线程管理并订阅
    private void toSubscribe(Observable o, Subscriber s){
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s);
    }
}
